package liuliu.throughwaring.ui.activity;

import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.settting.ISettingView;
import liuliu.throughwaring.control.settting.SettingListener;
import liuliu.throughwaring.model.BlueToothModel;
import liuliu.throughwaring.model.PoliceModel;

/**
 * Created by Administrator on 2015/11/28.
 */
public class SysSettingActivity extends BaseActivity implements ISettingView {
    @CodeNote(id = R.id.sys_setting_toolbar)
    TToolbar toolbar;
    @CodeNote(id = R.id.sys_modiffy_password, click = "onClick")
    TextView sys_modiffy_password;
    @CodeNote(id = R.id.sys_setting_ip, click = "onClick")
    TextView sys_setting_ip;
    @CodeNote(id = R.id.sys_right_menu_draw)
    DrawerLayout right_menu;//右侧菜单
    @CodeNote(id = R.id.right_menu_police_search_ll)
    LinearLayout right_menu_police_search_ll;
    @CodeNote(id = R.id.right_menu_unit_search_ll)
    LinearLayout right_menu_unit_search_ll;
    @CodeNote(id = R.id.right_menu_person_search_ll)
    LinearLayout right_menu_person_search_ll;
    @CodeNote(id = R.id.sys_txt_bluetooth)
    TextView sys_txt_bluetooth;
    @CodeNote(id = R.id.sys_person_cardid, click = "onClick")
    TextView sys_personcardid;
    @CodeNote(id = R.id.right_menu_listview)
    ListView mRightMenu;
    @CodeNote(id = R.id.right_menu_toolbar)
    TToolbar right_menu_toolbar;
    @CodeNote(id = R.id.sys_setting_bluetooth, click = "onClick")
    RelativeLayout sys_setting_bluetooth;
    private String[] blue_device_scale;
    List<BlueToothModel> listblue;
    SettingListener listener;
    Dialog dialog;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_sys_setting);
        listener = new SettingListener(this, this);
        sys_txt_bluetooth.setText(Utils.ReadString("BlueToothName"));
        getBluetooth();
    }

    @Override
    public void initEvents() {
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        //选择文本信息点击事件
        sys_setting_bluetooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerManager();
            }
        });
        mRightMenu.setVisibility(View.VISIBLE);
        right_menu_unit_search_ll.setVisibility(View.GONE);
        right_menu_person_search_ll.setVisibility(View.GONE);
        right_menu_police_search_ll.setVisibility(View.GONE);
        right_menu_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        if (listblue == null) {
            sys_setting_bluetooth.setVisibility(View.GONE);
        } else {
            //左划菜单的Adapter
            mRightMenu.setAdapter(new CommonAdapter<BlueToothModel>(SysSettingActivity.this, listblue,
                    R.layout.right_menu_item) {
                @Override
                public void convert(final ViewHolder holder, final BlueToothModel model, int position) {
                    holder.setText(R.id.right_menu_title, model.getDeviceName());
                }
            });
            //左划菜单项点击事件
            mRightMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    drawerManager();
                    sys_txt_bluetooth.setText(listblue.get(position).getDeviceName());
                    Utils.WriteString("BlueToothAddress", listblue.get(position).getDeviceAddress());
                    Utils.WriteString("BlueToothName", listblue.get(position).getDeviceName());
                }
            });
        }
    }

    /*开启关闭左划菜单*/
    private void drawerManager() {
        if (right_menu.isDrawerOpen(GravityCompat.END)) {
            right_menu.closeDrawer(GravityCompat.END);
        } else {
            right_menu.openDrawer(GravityCompat.END);
            toolbar.setCenterTitle("选择民警");
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sys_person_cardid:
                //下载在逃人员库
                dialog = Utils.ProgressDialog(SysSettingActivity.this, dialog, "下载中，请稍候...", false);
                dialog.show();
                listener.getCards();
                break;
            case R.id.sys_modiffy_password:
                mUtils.IntentPost(ModifyPasswordActivity.class);
                break;
            case R.id.sys_setting_ip:
                mUtils.IntentPost(SettingActivity.class);
                break;
            case R.id.sys_setting_bluetooth:
                break;
        }
    }

    private void getBluetooth() {
        BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBtAdapter == null) {
            mUtils.ToastShort(SysSettingActivity.this, "不支持蓝牙设备！");
        } else {
            Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
            if (pairedDevices.size() == 0) {
                mUtils.ToastShort(SysSettingActivity.this, "没有配对蓝牙设备！");
                startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
            }
            blue_device_scale = new String[pairedDevices.size()];
            int count = 0;
            listblue = new ArrayList<BlueToothModel>();
            for (BluetoothDevice device : pairedDevices) {
                BlueToothModel blue = new BlueToothModel();
                blue.setDeviceName(device.getName());
                blue.setDeviceAddress(device.getAddress());
                listblue.add(blue);
                blue_device_scale[count++] = device.getName() + "\n" + device.getAddress();
            }
        }
    }

    @Override
    public void downLoad(boolean isTrue, String mes) {
        dialog.dismiss();
        if (isTrue) {
            mUtils.ToastShort(SysSettingActivity.this, "在逃人员库下载成功！");
        } else {
            mUtils.ToastShort(SysSettingActivity.this, "在逃人员库下载失败！");
        }


    }

    @Override
    public void DownKu(boolean isTrue) {

    }
}
